package work.mediway.common.transport;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mediway.hos.base.exception.SysExceptionEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ClassName DataCollectionPage
 * @Description TODO
 * @Author dh_lei
 * @Date 2023/9/11 8:51
 * @Version 1.0
 */
@Slf4j
@ToString
@NoArgsConstructor
@Data
public class DataCollectionResponse<T>  {

    private String code;
    private String msg;
    private Data data;
    private long total;
    private long size;
    private long current;

    private long pages;
    private boolean success;

    public static class Data<T> {
        private List<T> records;
        private long totalNum=0;

        public List<T> getRecords() {
            return records;
        }
        public void setRecords(List<T> records) {
            this.records = records;
        }

        public long getTotalNum() {
            return totalNum;
        }
        public void setTotalNum(long totalNum) {
            this.totalNum = totalNum;
        }
    }

    public static DataCollectionResponse success(Page data) {
        return new DataCollectionResponse(SysExceptionEnum.SUCCESS.getCode(), SysExceptionEnum.SUCCESS.getMsg(),data);
    }

    public static DataCollectionResponse success(Page data,long total) {
        return new DataCollectionResponse(SysExceptionEnum.SUCCESS.getCode(), SysExceptionEnum.SUCCESS.getMsg(),data,total);
    }


    protected DataCollectionResponse(String code, String message, Page pageData) {
        if (ObjectUtil.isEmpty(pageData)) {
            pageData = new Page();
        }
        Data data=new Data();
        data.setRecords(pageData.getRecords());
        this.setCode(code);
        this.setMsg(message);
        this.setData(data);
        this.setTotal(pageData.getTotal());
        this.setCurrent(pageData.getCurrent());
        this.setSize(pageData.getSize());
        this.setPages(pageData.getPages());
        this.setSuccess(isSuccess());
    }

    protected DataCollectionResponse(String code, String message, Page pageData,long totalNum) {
        if (ObjectUtil.isEmpty(pageData)) {
            pageData = new Page();
        }
        Data data=new Data();
        data.setRecords(pageData.getRecords());
        data.setTotalNum(totalNum);
        this.setCode(code);
        this.setMsg(message);
        this.setData(data);
        this.setTotal(pageData.getTotal());
        this.setCurrent(pageData.getCurrent());
        this.setSize(pageData.getSize());
        this.setPages(pageData.getPages());
        this.setSuccess(isSuccess());
    }




    public boolean isSuccess() {
        return SysExceptionEnum.SUCCESS.getCode().equals(this.code);
    }






}
